﻿jQuery(function ($) {
    var $quickLibLists = $('div.s4-ql ul li ul');
    //Set the Images up    
    var Collapse = "/_layouts/images/collapse.gif";
    var Expand = "/_layouts/images/expand.gif";

    //Hide all
    $quickLibLists.hide();
    //Find each top level UI and add reletive buttons & children numbers     
    $quickLibLists.each(function() {
        var $this = $(this);        
        $this.parent().find('a:first span.menu-item-text').append(['<a class=\'min\'><img src=\'/_layouts/images/expand.gif\'/></a><span class=\'min-nbr\'>(', $this.children().length, ')</span>'].join(''));
    });

    //Setup Click Hanlder    
    $('a.min').click(function() {        
    //Get Reference to img        
        var img = $(this).children();        
        //Traverse the DOM to find the child UL node        
        var subList = $(this).parents("a.menu-item").next();
        //Check the visibility of the item and set the image       
        var Visibility = subList.is(":visible")? img.attr('src',Expand) : img.attr('src',Collapse);
        //Toggle the UL        
        subList.slideToggle();

        return false;
    });
});